﻿var timerID = 0;
var totalSeconds = 60;
var targetField = null;
var hiddenField = null;
var hiddenMinutes = 0;

function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      timerID = 0;
   }

   totalSeconds++;

   var tHr = padValue(Math.floor(totalSeconds/360));
   var tMin = padValue(Math.floor(totalSeconds/60));
   var tSec = padValue(totalSeconds%60);
   
   targetField.value = "" + tHr + ":" + tMin + ":" + tSec;
   hiddenField.value = tMin;
   
   timerID = setTimeout("UpdateTimer()", 1000);
}

function padValue(num) {
    num += '';
    if(num.length < 2) return "0" + num;
    else return num;
}

function StartTimer(field, hidden) {
   targetField = field;
   hiddenField = hidden;

   targetField.value = "00:00:00";
   hiddenField.value = "0";

   timerID  = setTimeout("UpdateTimer()", 1000);
}

function StopTimer() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }

   targetField = null;
   hiddenField = null;
}

function PauseTimer() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }    
}

function Reset() {
   totalSeconds = 0;

   targetField.value = "00:00:00";
}